package com.study.custom.mybatis;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/12/23 21:37
 */
public class XMLConfigBuilder {

    public static Configuration loadConfiguration(InputStream is){
        try {
            //1）数据库配置信息存储
            Configuration cfg = new Configuration();
            //创建SAXReader对象读取XML文件字节输入流
            SAXReader reader = new SAXReader();
            Document document = reader.read(is);
            //解析配置文件,获取根节点信息,//property表示获取根节点下所有的property结点对象
            List<Element> rootList = document.selectNodes("//property");
            //循环迭代所有结点对象
            for (Element element : rootList) {
                //name属性的值
                String name = element.attributeValue("name");
                //vallue属性的值
                String value = element.attributeValue("value");

                //2）将解析的数据库连接信息存储到Configuration中
                //数据库驱动
                if(name.equals("driver")){
                    cfg.setDriver(value);
                }else if(name.equals("url")){
                    //数据库连接地址
                    cfg.setUrl(value);
                }else if(name.equals("username")){
                    //数据库账号
                    cfg.setUsername(value);
                }else if(name.equals("password")){
                    //数据库密码
                    cfg.setPassword(value);
                }
            }

            //获取需要解析的XML路径
            List<Element> list = document.selectNodes("//mapper");
            for (Element element : list) {
                String resource = element.attributeValue("resource");
                Map<String, Mapper> loadMappers = loadMapper(resource);
                cfg.setMappers(loadMappers);
            }

            return cfg;
            //拿到映射配置文件的路径
            //接下来看下面的分析准备解析映射配置文件
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解析UserMapper.xml，提取SQL语句和返回JavaBean全限定名
     * path为UserMapper.xml的路径
     */
    public static Map<String, Mapper> loadMapper(String path) {
        Map<String, Mapper> mappers = new HashMap<String, Mapper>();
        SAXReader reader = new SAXReader();
        try {
            InputStream is = Resources.getResourceAsStream(path);
            Document mapperDocument = reader.read(is);
            Element rootElement = mapperDocument.getRootElement();
            String namespace = rootElement.attributeValue("namespace");

            List<Element> selectNodes = mapperDocument.selectNodes("//select");
            for (Element selectNode : selectNodes) {
                String id = selectNode.attributeValue("id");
                Attribute id1 = selectNode.attribute("id");
                System.out.println(id1.toString());
                String resultType = selectNode.attributeValue("resultType");
                String key = namespace + "." + id;
                // 获取sql语句
                String sql = selectNode.getText();
                Mapper mapper = new Mapper(sql, resultType);
                mappers.put(key, mapper);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return mappers;

    }
}
